home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
aminet
/
util
/
pack
/
drdobbscompress.lha
/
entropy.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-20
|
2KB
|
76 lines
#include <stdio.h>
#include <math.h>
#define LOG(x) 3.32 * log10(x)
#define ENTROPY(x) -(x*LOG(x))
FILE *in;
unsigned int table[256];
void read_input(void);
double analyze(void);
void usage(void);
main (argc, argv)
char **argv;
{
double result;
if (argc==1) usage();
in = fopen(*++argv,"r");
if (!in) printf("\nCouldn't open input file.");
if (!in) exit(-1);
printf("\n ** Reading file ... **");
read_input();
printf("\n ** Calculating ... **\n");
result = analyze();
printf("\n The file \"%s\" has zero-order",*argv);
printf("\n entropy of %1u.%2u bits per byte.\n",(int)(result),(int)(100*result-100*(int)(result)));
printf("\n Approximate shrinkage potential");
printf("\n using Huffman techniques : ");
printf(" %3u%%\n\n\n",(int)(100-(result*100)/8));
fclose(in);
return(0L);
}
void read_input()
{
int ch;
while ((ch=getc(in)) != EOF)
table[ch]++;
}
double analyze()
{
double accum = 0.0;
double freq;
long fsize = 0L;
register int z;
fsize = ftell(in);
for (z=0; z<256; z++)
if (table[z])
{
freq = (double) table[z]/fsize;
accum += (double) ENTROPY(freq);
}
return accum;
}
void usage()
{
printf("\n\n");
printf(" Entropy v1.00 by Kas Thomas. Public Domain.\n\n");
printf(" Syntax: ENTROPY {filename} [Enter]\n\n");
printf(" Entropy is a measure of information storage efficiency.\n");
printf(" This program calculates a file's entropy, hence its\n");
printf(" compressibility, using the entropy equation of Shannon.\n");
printf(" (see \"Information Theory : Symbols, Signals, & Noise,\"\n");
printf(" by John Pierce, Dover, 1981).\n\n");
exit(0L);
}